package com.xmh.code.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class PingService {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Value("${mq-config.topic}")
    private String topic;

    @Scheduled(fixedRate = 500)
    public void pingPongService() {
        pingService("A");
    }

    @Scheduled(fixedRate = 700)
    public void pingPongService2() {
        pingService("B");
    }

    @Scheduled(fixedRate = 800)
    public void pingPongService3() {
        pingService("C");
    }

    @Scheduled(fixedRate = 900)
    public void pingPongService4() {
        pingService("D");
    }

    public void pingService(String threadName) {
        this.rocketMQTemplate.sendOneWay(topic, MessageBuilder.withPayload(threadName).build());
    }
}
